package com.gambi.tapestry.client.chatter.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gambi.tapestry.client.chatter.model.Contact;
import com.gambi.tapestry.client.chatter.model.ContactList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowCloseListener;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;

public class MessengerView
// This allows us to define a custom public interface for our view without
		// exposing
		// another widgets unneeded methods
		extends Composite
// These two interfaces require the class to implement methods that will receive
		// events
		// from the browser to handle resizing and closing events
		implements WindowCloseListener, WindowResizeListener {

	// Main panel can have many child widgets but displays only one widget at a
	// time.
	private DeckPanel mainPanel = new DeckPanel();

	// View classes
	private ContactListView contactListView;
	private SignInView signIn;
	private SignOutView signOut;
	private ContactList contactList;

	// Internal state of the view
	private List openChatWindows = new ArrayList();
	private Map allChatWindows = new HashMap();

	// Event listener. Note this is important
	private MessengerViewListener listener;

	public MessengerView(MessengerViewListener listener) {
		// Initialize Composite
		initWidget(mainPanel);
		// Initialize listener
		this.listener = listener;
		// Add ad visualize the sign in view
		signIn = new SignInView(this);
		mainPanel.add(signIn);
		mainPanel.showWidget(0);

		// Register for the window events
		Window.addWindowCloseListener(this);
		Window.addWindowResizeListener(this);
	}

	public MessengerViewListener getListener() {
		return this.listener;
	}

	public ContactList getContactList() {
		return this.contactList;
	}

	// WindowCloseListener ... is SignIn or MessageView that implements this ?
	public String onWindowClosing() {
		listener.onSignOut();
		return null;
	}

	public void onWindowClosed() {
	}

	// WindowResizeListener
	public void onWindowResized(int width, int height) {
		resetChatWindowPositions();
	}

	protected void resetChatWindowPositions() {
		for (int i = 0; i < openChatWindows.size(); ++i) {
			ChatWindowView chatWindow = (ChatWindowView) openChatWindows.get(i);
			chatWindow.setPosition(i);
		}
	}

	public void closeChatWindow(ChatWindowView windowToClose) {
		openChatWindows.remove(windowToClose.position);
		windowToClose.setVisible(false);
		windowToClose.position = -1;
		resetChatWindowPositions();
	}

	public ChatWindowView getChatWindowView(Contact contact) {
		ChatWindowView chatWindow = (ChatWindowView) allChatWindows.get(contact
				.getName());
		if (chatWindow == null) {
			chatWindow = new ChatWindowView(this, contact);
			allChatWindows.put(contact.getName(), chatWindow);
		}
		if (chatWindow.position == -1) {
			chatWindow.position = openChatWindows.size();
			openChatWindows.add(chatWindow);
		}
		chatWindow.show();
		return chatWindow;
	}

	public void setContactList(ContactList contactList) {
		this.contactList = contactList;
		if (contactListView == null) {
			contactListView = new ContactListView(this);
			mainPanel.add(contactListView);
		}
		mainPanel.showWidget(1);
	}

	public void signOut(){
		SignOutView signOutView = new SignOutView( this );
		mainPanel.add( signOutView );
		mainPanel.showWidget( 2 );
	}
	public ContactListView getContactListView() {
		return contactListView;
	}

	public void reset() {
		signIn.reset();
		mainPanel.showWidget(0);
		// Reverse order ?
		mainPanel.remove(2);
		mainPanel.remove(1);
		// Reset the view
		contactListView = null;
		signOut = null;
		
	}

}
